package com.suredy.base.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

@Entity
@Table(name = "V_E_USER")
public class User {

	private String id;
	private String orgCode;
	private String userCode;
	private String unitCode;
	private String userNo;
	private String shortName;
	private String fullName;
	private String pwd;
	private Unit unit;

	@Id
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOrgCode() {
		return this.orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getUserCode() {
		return this.userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getUnitCode() {
		return this.unitCode;
	}

	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}

	public String getUserNo() {
		return this.userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public String getShortName() {
		return this.shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getFullName() {
		return this.fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getPwd() {
		return this.pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@ManyToOne
	@JoinColumn(name = "unitId")
	public Unit getUnit() {
		return this.unit;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	@Transient
	public String getFullUnitName() {
		String sn = this.getShortName();
		String fn = this.getFullName();

		if (StringUtils.isBlank(sn))
			sn = "";

		if (StringUtils.isBlank(fn))
			fn = "";

		return fn.replace(sn + "/", "");
	}

	@Transient
	public String getShortUnitName() {
		String fn = this.getFullUnitName();

		int index = -1;

		if (StringUtils.isBlank(fn) || (index = fn.indexOf('/')) < 0)
			return fn;

		return fn.substring(0, index);
	}

}
